package com.xiaoyao.novel.proxy;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestIpCrawl {
	static class IpCount implements Comparable<IpCount>{
		public String ip;
		public int count = 0;
 
		public IpCount(String ip) {
			super();
			this.ip = ip;
		}
		public void countPlus() {
			count++;
		}
		@Override
		public int hashCode() {
			return ip.hashCode();
		}
		public int compareTo(IpCount o) {
			IpCount ipCount=(IpCount)o;
			return this.count>ipCount.count?1:(this.count==ipCount.count?0:-1); 
		}
	}

	static String logFileName = "F:/赚钱软件/logs/novel.log";
	static int MAX_COUNT=30 ;
	public static void test() {
		Map<String, IpCount> ipCountMap = new HashMap<String, IpCount>();
		FileInputStream fis = null;
		BufferedReader reader = null;
		try {
			fis = new FileInputStream(logFileName);
			reader = new BufferedReader(new InputStreamReader(fis, "GBK"));
			String line = null;
			// WARN [StaticFilter:doFilter-70][17:52:21]
			// 访问IP:182.110.218.244;页面:/book/355.html
			String regex = ".+?访问IP:(.+?);页面.+";
			Pattern pattern = Pattern.compile(regex);
			while ((line = reader.readLine()) != null) {
				Matcher matcher = pattern.matcher(line);
				if (matcher.find()) {
					String ip = matcher.group(1);
					IpCount ipCount = ipCountMap.get(ip);
					if (ipCount == null) {
						ipCount = new IpCount(ip);
					}
					ipCount.countPlus();  
					ipCountMap.put(ip, ipCount);
				}
			}
			System.out.println("过滤");
			// 小于30次一下的过滤掉
			Set<String> keyset = new HashSet<String>( ipCountMap.keySet() );
			for(String ip :keyset){
				if (ipCountMap.get(ip).count<MAX_COUNT) {
					ipCountMap.remove(ip);
				}
			}
			System.out.println("排序");
			List<IpCount> ipCountList =new ArrayList<IpCount>(ipCountMap.values());
			Collections.sort(ipCountList);
			Collections.reverse(ipCountList);
			for(IpCount ipCount:ipCountList){
				System.out.println("访问数:" + ipCount.ip + ",IP数量:" + ipCount.count);
			} 
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		}

	}
	public static void main(String[] args) {
		test();
	}
}
